package de.lizenzverwaltung.actions;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import de.lizenzverwaltung.core.ProduktgruppeService;
import de.lizenzverwaltung.core.ProduktgruppeServiceFactory;
import de.lizenzverwaltung.core.UserService;
import de.lizenzverwaltung.core.UserServiceFactory;
import de.lizenzverwaltung.model.dao.ProduktDAO;
import de.lizenzverwaltung.model.dao.ProduktDAOFactory;
import de.lizenzverwaltung.model.dao.ProduktgruppeDAO;
import de.lizenzverwaltung.model.dao.ProduktgruppeDAOFactory;
import de.lizenzverwaltung.model.dao.UserDAO;
import de.lizenzverwaltung.model.dao.UserDAOFactory;


public class UserAction extends HttpServlet {

	private static final long serialVersionUID = 3859148664063809698L;

	private Log logger = LogFactory.getLog(UserAction.class) ;
	private UserDAO userDAO = UserDAOFactory.getInstance();
	private UserService userService = UserServiceFactory.getInstance();
	
	@Override
	public void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {		
		logger.debug("entering doGet") ;
		HttpSession sess = req.getSession();
		sess.setAttribute("userListe", this.userDAO.getAllUsers());
	}
	
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {		
		logger.debug("entering doPost") ;
		
		// Formulardaten auslesen
		String methode = req.getParameter("methode");
		String value = req.getParameter("value");
		// Session holen
		HttpSession sess = req.getSession();
		
		if(methode.equals("berechtigung")){
			String uid = value.substring(7, 11);
			if(userService.aendernBerechtigung(uid)){
				resp.setContentType("text/plain");  
				resp.setCharacterEncoding("UTF-8"); 
				resp.getWriter().write("SUCCESS");	
			}else{
				resp.setContentType("text/plain");  
				resp.setCharacterEncoding("UTF-8"); 
				resp.getWriter().write("FAIL");	
			}			
		}
		
	}
}
